import worker from '@ohos.worker';
import testNapi from 'libentry.so'

// 创建worker线程中与主线程通信的对象
const parentPort = worker.parentPort

// 注册输入法弹出的回调
testNapi.setIMGShowMethod((msg: string) => {
    console.info(`jiangtao -> ${msg}`)
    parentPort.postMessage({ type: 'IMG', data: msg })
})

// worker线程接收主线程信息
parentPort.onmessage = function (e) {

    console.log(JSON.stringify(e))

    let data = e.data;

    switch (data.type) {
        case 'IMG': {
            // 输入完成，设置用户输入
            testNapi.setInputMessage(data.data)
            break;
        }
        case 'test': {
            // 测试函数，模拟native侧调用
            testNapi.test()
            break;
        }
    }
}

// worker线程发生error的回调
parentPort.onerror = function (e) {
    console.log("worker.js onerror");
}